//===============================================================================
// Microsoft Architecture Strategy Team
// LitwareHR - SaaS Sample Application
//===============================================================================
// Copyright  Microsoft Corporation.  All rights reserved.
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY
// OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT
// LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
// FITNESS FOR A PARTICULAR PURPOSE.
//===============================================================================
// The example companies, organizations, products, domain names,
// e-mail addresses, logos, people, places, and events depicted
// herein are fictitious.  No association with any real company,
// organization, product, domain name, email address, logo, person,
// places, or events is intended or should be inferred.
//===============================================================================


namespace Shp.Runtime.Sitka.ServiceAgent.Managers
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Diagnostics;
    using System.ServiceModel;
    using Shp.Runtime.Sitka.ServiceGateway.SitkaClient;
    using Shp.Runtime.Sitka.ServiceAgent.Factories;

    public static class ContainerManager
    {
        // Creates a Container
        public static void CreateContainer(string tenantId)
        {
            ProviderFactory<ITenantManager, DefaultTenantManager> factory = new ProviderFactory<ITenantManager, DefaultTenantManager>();
            CreateContainer( tenantId, factory.Build( "TenantManager" ) );
        }
        
        public static void CreateContainer( string tenantId, ITenantManager tenantManager )
        {
            try
            {
                ISitkaSoapService proxy = SitkaProxyFactory.BuildProxy( tenantManager );

                Scope scope = new Scope();
                scope.AuthorityId = tenantManager.GetAuthorityId( tenantId );

                Container c = new Container();
                c.Id = tenantManager.GetContainerId(tenantId);

                proxy.Create(scope, c);

                SitkaSoapServiceClient stratusServiceClient = proxy as SitkaSoapServiceClient;
                if (stratusServiceClient != null)
                {
                    stratusServiceClient.Close();
                }
            }
            catch (FaultException<Error> ex)
            {
                Debug.WriteLine(ex.Message);
                throw;
            }
        }

        public static void DeleteContainer(string tenantId)
        {
            ProviderFactory<ITenantManager, DefaultTenantManager> factory = new ProviderFactory<ITenantManager, DefaultTenantManager>();
            DeleteContainer(tenantId, factory.Build("TenantManager"));
        }
        
        public static void DeleteContainer(string tenantId, ITenantManager tenantManager)
        {
            try
            {
                ISitkaSoapService proxy = SitkaProxyFactory.BuildProxy( tenantManager );

                Scope scope = new Scope();
                scope.AuthorityId = tenantManager.GetAuthorityId(tenantId);
                scope.ContainerId = tenantManager.GetContainerId(tenantId);
                proxy.Delete(scope);

                SitkaSoapServiceClient stratusServiceClient = proxy as SitkaSoapServiceClient;
                if (stratusServiceClient != null)
                {
                    stratusServiceClient.Close();
                }
            }
            catch (FaultException<Error> ex)
            {
                Debug.WriteLine(ex.Message);
            }
        }


        public static bool ContainerExistsForTenant( string tenantId )
        {
            ProviderFactory<ITenantManager, DefaultTenantManager> factory = new ProviderFactory<ITenantManager, DefaultTenantManager>();
            return ContainerExistsForTenant( tenantId, factory.Build("TenantManager" ) );
        }

        public static bool ContainerExistsForTenant( string tenantId, ITenantManager tenantManager )
        {
            Entity containerEntity = null;
            try
            {
                ISitkaSoapService proxy = SitkaProxyFactory.BuildProxy( tenantManager );

                Scope scope = new Scope();
                scope.AuthorityId = tenantManager.GetAuthorityId( tenantId );
                scope.ContainerId = tenantManager.GetContainerId( tenantId );
                containerEntity = proxy.Get(scope);

                SitkaSoapServiceClient stratusServiceClient = proxy as SitkaSoapServiceClient;
                if (stratusServiceClient != null)
                {
                    stratusServiceClient.Close();
                }

            }
            catch (FaultException<Error> ex)
            {
                Debug.WriteLine(ex.Message);                
            }
            finally
            {               
            }

            return (containerEntity != null);
        }
    }
}
